package com.eam.asset.category.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eam.asset.category.dto.AssetCategoryAddDto;
import com.eam.asset.category.dto.AssetCategoryDto;
import com.eam.asset.category.util.CategoryUtil;
import com.eam.asset.file.utils.AssetEncode;
import com.eam.common.constant.Constants;
import com.eam.common.utils.DateUtils;
import com.eam.common.utils.SecurityUtils;
import com.eam.common.utils.uuid.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.eam.asset.category.mapper.AssetCategoryMapper;
import com.eam.asset.category.domain.AssetCategory;
import com.eam.asset.category.service.IAssetCategoryService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 资产档案类别信息Service业务层处理
 *
 * @author tzf
 * @date 2023-10-31
 */
@Service
public class AssetCategoryServiceImpl extends ServiceImpl<AssetCategoryMapper, AssetCategory> implements IAssetCategoryService {

    /**
     * 查询资产档案类别信息
     *
     * @param categoryId 资产档案类别信息主键
     * @return 资产档案类别信息
     */
    @Override
    public AssetCategory selectAssetCategoryByCategoryId(Long categoryId) {
        //todo 考虑做缓存
        AssetCategory assetCategory = baseMapper.selectById(categoryId);
        AssetCategoryDto assetCategoryDto = new AssetCategoryDto();
        BeanUtils.copyProperties(assetCategory, assetCategoryDto);
        assetCategoryDto.setType(CategoryUtil.Generate(assetCategoryDto.getType()));
        return assetCategoryDto;
    }

    /**
     * 查询资产档案类别信息列表
     *
     * @param assetCategory 资产档案类别信息
     * @return 资产档案类别信息
     */
    @Override
    public List<AssetCategoryDto> selectAssetCategoryList(AssetCategory assetCategory) {
        //todo 考虑做缓存
        List<AssetCategory> assetCategories = baseMapper.selectAssetCategoryList(assetCategory);
        if (ObjectUtils.isEmpty(assetCategories)) return null;
        assetCategories.forEach(t -> t.setType(CategoryUtil.Generate(t.getType())));
        return assetCategories.stream().map(CategoryUtil::DtoTransform).collect(Collectors.toList());
    }

    /**
     * 新增资产档案类别信息
     *
     * @param assetCategory 资产档案类别信息
     * @return 结果
     */
    @Override
    public int insertAssetCategory(AssetCategory assetCategory) {
        AssetCategoryAddDto assetCategoryAddDto = new AssetCategoryAddDto();
        BeanUtils.copyProperties(assetCategory, assetCategoryAddDto);
        assetCategoryAddDto.setCreateTime(DateUtils.getNowDate());
        assetCategoryAddDto.setUpdateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        assetCategoryAddDto.setCategoryId(IdUtils.simpleUUID());
        StringBuffer encode = AssetEncode.getEncode(assetCategoryAddDto.getType()).
                append(AssetEncode.getEncodeByName(assetCategoryAddDto.getCategoryName())).
                append(DateUtils.dateTime());
        assetCategoryAddDto.setCategoryCode(encode.toString());
        return baseMapper.insert(assetCategoryAddDto);
    }

    /**
     * 修改资产档案类别信息
     *
     * @param assetCategory 资产档案类别信息
     * @return 结果
     */
    @Override
    public int updateAssetCategory(AssetCategory assetCategory) {
        assetCategory.setUpdateTime(DateUtils.getNowDate());
        assetCategory.setUpdateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        AssetCategoryDto assetCategoryDto = new AssetCategoryDto();
        BeanUtils.copyProperties(assetCategory, assetCategoryDto);
        return baseMapper.updateById(assetCategoryDto);
    }

    /**
     * 批量删除资产档案类别信息
     *
     * @param categoryIds 需要删除的资产档案类别信息主键
     * @return 结果
     */
    @Override
    public int deleteAssetCategoryByCategoryIds(Long[] categoryIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(categoryIds));
    }

    /**
     * 删除资产档案类别信息信息
     *
     * @param categoryId 资产档案类别信息主键
     * @return 结果
     */
    @Override
    public int deleteAssetCategoryByCategoryId(Long categoryId) {
        return baseMapper.deleteById(categoryId);
    }

    @Override
    public List<AssetCategoryDto> GetRootCategoryTypeList(String type) {
        LambdaQueryWrapper<AssetCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.hasText(type), AssetCategory::getParentId, Constants.ZERO).
                eq(StringUtils.hasText(type), AssetCategory::getType, type);
        List<AssetCategory> assetCategories = baseMapper.selectList(queryWrapper);
        if (ObjectUtils.isEmpty(assetCategories)) return null;
        return assetCategories.stream().map(CategoryUtil::DtoTransform).collect(Collectors.toList());
    }
}
